//用于创建整个应用的路由器
import Router from 'vue-router'
//引入组件
import Login from '../components/Login.vue'
import Register from "../components/Register.vue";
import Home from "@/components/Home.vue";
import Welcome from "../components/user/Welcome.vue";
import UserTable from "@/components/admin/UserTable.vue";
import HotelTable from "@/components/admin/HotelTable.vue";
import RoomTable from "@/components/admin/RoomTable.vue";
import OrderTable from "@/components/admin/OrderTable.vue";
import MyOrder from "@/components/user/MyOrder.vue";
import RoomDisplay from "@/components/user/RoomDisplay.vue";
import EditInfo from "../components/user/EditInfo.vue";
import Log from "@/components/superAdmin/Log.vue";

//创建一个路由器
const router=new Router({
    routes:[
        {path:'/',redirect:'/login'},
        {path:'/login',name:'login',component:Login,},
        {path:'/register',name:'register',component:Register,},
        {
            path:'/home',
            name:'home',
            component:Home,
            redirect:'/welcome',
            children:[
                {path:'/welcome',name:'welcome',component:Welcome},
                {path:'/userTable',name:'userTable',component:UserTable},
                {path:'/hotelTable',name:'hotelTable',component:HotelTable},
                {path:'/roomTable',name:'roomTable',component:RoomTable},
                {path:'/orderTable',name:'orderTable',component:OrderTable},
                {path:'/myOrder',name:'myOrder',component:MyOrder},
                {path:'/roomDisplay',name:'roomDisplay',component:RoomDisplay},
                {path:'/editInfo',name:'editInfo',component:EditInfo},
                {path:'/logs',name:'logs',component:Log}
            ]
        },
    ]
})

//挂载路由导航守卫
router.beforeEach((to, from, next) => {
    //to 将要访问的路径
    //from 代表从哪个路径跳转而来
    //next 是一个函数，表示放行

    if (to.name === 'login'||to.name==='register') return next();
    //查看是否登录
    const user=window.sessionStorage.getItem('user');
    if (!user) return next('/login');
    next()
})

export default router